<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属班级" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入所属班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责老师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入负责老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="困难系数" prop="difficulty">
        <el-select v-model="queryParams.difficulty" placeholder="请选择困难系数" clearable>
          <el-option
            v-for="dict in dict.type.sys_difficult_coefficient"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模拟面试" prop="mockInterview">
        <el-select v-model="queryParams.mockInterview" placeholder="请选择模拟面试" clearable>
          <el-option
            v-for="dict in dict.type.sys_simulation_interview"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结业考试" prop="finalTest">
        <el-select v-model="queryParams.finalTest" placeholder="请选择结业考试" clearable>
          <el-option
            v-for="dict in dict.type.sys_final_test"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['eduadmin:repetition:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['eduadmin:repetition:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eduadmin:repetition:remove']"
        >移出留级</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eduadmin:repetition:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repetitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="所属班级" align="center" prop="className" />
      <el-table-column label="负责老师" align="center" prop="teacherName" />
      <el-table-column label="困难系数" align="center" prop="difficulty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_difficult_coefficient" :value="scope.row.difficulty"/>
        </template>
      </el-table-column>
      <el-table-column label="模拟面试" align="center" prop="mockInterview">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_simulation_interview" :value="scope.row.mockInterview"/>
        </template>
      </el-table-column>
      <el-table-column label="结业考试" align="center" prop="finalTest">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_final_test" :value="scope.row.finalTest"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eduadmin:repetition:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateTask(scope.row)"
            v-hasPermi="['eduadmin:repetition:task']"
          >编辑任务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eduadmin:repetition:remove']"
          >移出留级</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改留级管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属班级" prop="className">
          <!--          <el-input v-model="form.className" placeholder="请输入所属班级" />-->
          <el-select v-model="form.className" @change="classChange" placeholder="请输入所属班级">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
<!--          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />-->
          <el-select v-model="form.studentName" placeholder="请输入学生姓名">
            <el-option
              v-for="(item,index) in studentNameList"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责老师" prop="teacherName">
<!--          <el-input v-model="form.teacherName" placeholder="请输入负责老师" />-->
          <el-select v-model="form.teacherName" placeholder="请输入负责老师">
            <el-option
              v-for="item in teachers"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="困难系数" prop="difficulty">
          <el-select v-model="form.difficulty" placeholder="请选择困难系数">
            <el-option
              v-for="dict in dict.type.sys_difficult_coefficient"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟面试" prop="mockInterview">
          <el-select v-model="form.mockInterview" placeholder="请选择模拟面试">
            <el-option
              v-for="dict in dict.type.sys_simulation_interview"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业考试" prop="finalTest">
          <el-select v-model="form.finalTest" placeholder="请选择结业考试">
            <el-option
              v-for="dict in dict.type.sys_final_test"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 任务列表框 -->
    <el-dialog :title="title" :visible.sync="taskOpen" width="500px" append-to-body>
      <el-form ref="form" :model="taskForm" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="taskForm.studentName" :disabled="true" placeholder="学生姓名" />
<!--          <el-select v-model="taskForm.studentName" placeholder="学生姓名">-->
<!--          </el-select>-->
        </el-form-item>
        <el-form-item label="任务日期" prop="happenTime">
          <el-date-picker clearable
                          v-model="taskForm.happenTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务状态" prop="taskState">
          <el-select v-model="taskForm.taskState" placeholder="请选择任务状态">
            <el-option
              v-for="dict in dict.type.sys_task_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务说明" prop="taskdesc">
          <el-input v-model="taskForm.taskdesc" placeholder="请输入内容" />
        </el-form-item>
        <div>
          <el-button type="primary" @click="submitTaskForm">添加任务</el-button>
          <el-button @click="taskCancel">关闭</el-button>
        </div>
        <br>
        <el-timeline>
          <el-timeline-item v-for="o in studentTaskForm" :timestamp="o.happenTime" placement="top" >
            <el-card class="box-card" placement="top"  style="width: 360px">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-dite"
                  size="mini"
                  @click="handleUpdateHostiryTask(o)"
                >完成修改</el-button>
              </el-col>
              <br>
              <h4>负责老师: [{{ o.teacherName }}]</h4>
              <p>{{"任务说明:" + o.taskdesc}}</p>
              任务状态:
              <el-select v-model="o.taskState" placeholder="任务状态">
                <el-option
                  v-for="dict in dict.type.sys_task_state"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                ></el-option>
              </el-select>
<!--              <p>{{"完成说明:" + o.finishDesc}}</p>-->
              <br>完成说明:<el-input v-model="o.finishDesc" placeholder="完成说明" style="width: 200px"/>

            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { listRepetition, getRepetition, delRepetition, addTask, updateRepetition, addTaskRepetition ,getTaskRepetition,getStudentTaskRecord,findTaskList,update} from "@/api/eduadmin/repetition";
import {listClass} from "@/api/student/class";
import {getStudentByClassId, listStudentMessage} from "@/api/student/studentMessage";
import {listTeacherManagement} from "@/api/teacher/teacherManagement";

export default {
  name: "Repetition",
  dicts: ['sys_difficult_coefficient', 'sys_test_state','sys_task_state','sys_simulation_interview','sys_final_test'],
  data() {
    return {
      studentNameList: [],
      teachers: [],
      students: [],
      classList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留级管理表格数据
      repetitionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      taskOpen: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        className: null,
        teacherName: null,
        difficulty: null,
        mockInterview: null,
        finalTest: null
      },
      // 表单参数
      form: {},
      // 学生任务记录表单
      studentTaskForm: [{
        studentName: null,
        studentId: null,
        teacherName: null,
        taskState: null,
        taskdesc: null,
        happenTime: null,
        finishDesc: null,
      }],
      // 任务表单
      taskForm: {
        studentName: null,
        happenTime : null,
        taskState: null,
        taskdesc :null,
        studentId: null,
      },

      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getClassList();
    this.getStudentList();
    this.getTeacherList();
  },
  methods: {
    /** 根据班级id查询所有学生 */
    classChange(value) {
      getStudentByClassId().then(response => {
        this.test = response;
        for (let i in this.test) {
          if (this.test[i].name == value) {
            this.studentNameList = this.test[i].names;
          }
        }
        // console.log(this.studentNameList);
      });
    },

    /** 查询教师管理列表 */
    getTeacherList() {
      listTeacherManagement(this.queryParams).then(response => {
        this.teachers = response.rows;
      });
    },
    /* 查询所有学生 */
    getStudentList() {
      listStudentMessage().then(response => {
        this.students = response.rows;
        // console.log(this.students)
      });
    },

    /** 查询班级管理列表 */
    getClassList() {
      listClass().then(response => {
        this.classList = response.rows;
      });
    },

    /** 查询留级管理列表 */
    getList() {
      this.loading = true;
      listRepetition(this.queryParams).then(response => {
        this.repetitionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    taskCancel() {
      this.taskOpen = false;
      this.taskReset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        className: null,
        teacherName: null,
        difficulty: null,
        mockInterview: null,
        finalTest: null
      };
      this.resetForm("form");
    },

    // 任务表单重置 taskReset
    taskReset() {
      this.taskForm = {
        studentName: null,
        happenTime : null,
        taskState: null,
        taskdesc :null,
        studentId: null,
      }
      this.resetForm("taskForm");
    },
    // 历史任务表单重置
    studentTaskReset() {
      this.studentTaskForm = {
        studentName: null,
        studentId: null,
        teacherName: null,
        taskState: null,
        taskdesc: null,
        happenTime: null,
        finishDesc: null,
      }
      this.resetForm("studentTaskForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加留级管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepetition(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改留级管理";
      });
    },

    // handleUpdateHostiryTask
    handleUpdateHostiryTask(row) {
      console.log("11111")
      console.log(row)
      this.reset();
      const id = row.id || this.ids
      update(row).then(response => {
        this.open = false;
      });
    },

    // handleUpdateTask 编辑任务状态按钮
    handleUpdateTask(row) {
      this.taskReset();
      const id = row.id || this.ids

      findTaskList(row.studentId).then(response => {
        this.studentTaskForm = response;
        // console.log("1111")
        // console.log(this.studentTaskForm)
      });
      getTaskRepetition(id).then(response => {
        this.taskForm = response;
        // console.log(this.taskForm)
        this.taskOpen = true;
        this.title = [this.taskForm.studentName] + "的历史任务";
      });


    },


    // 添加任务按钮  submitTaskForm
    submitTaskForm() {
      addTask(this.taskForm).then(response => {
          this.$modal.msgSuccess("新增任务成功");
          this.taskOpen = false;
          this.getList();
        });

    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepetition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTaskRepetition(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除留级管理编号为"' + ids + '"的数据项？').then(function() {
        return delRepetition(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eduadmin/repetition/export', {
        ...this.queryParams
      }, `repetition_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
}
</style>
